package com.jhx.video.utils

import android.support.design.widget.AppBarLayout

/**
 * 监听toolbar展开与折叠
 *
 * @author Jack 2018-03-27 16:44
 */
abstract class AppBarStatusChangeListener : AppBarLayout.OnOffsetChangedListener {
    private var mCurrentState = IDLE

    companion object {
        val EXPANDED = 1
        val COLLAPSED = 2
        val IDLE = 3
    }

    override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {
        when {
            verticalOffset == 0 -> {
                if (mCurrentState != EXPANDED) {
                    onStateChanged(appBarLayout, EXPANDED)
                }
                mCurrentState = EXPANDED
            }
            Math.abs(verticalOffset) >= appBarLayout.totalScrollRange -> {
                if (mCurrentState != COLLAPSED) {
                    onStateChanged(appBarLayout, COLLAPSED)
                }
                mCurrentState = COLLAPSED
            }
            else -> {
                if (mCurrentState != IDLE) {
                    onStateChanged(appBarLayout, IDLE)
                }
                mCurrentState = IDLE
            }
        }
    }

    abstract fun onStateChanged(appBarLayout: AppBarLayout, state: Int)
}